package com.test.service.impl;

import com.test.controller.Admininfo.MyObject;
import com.test.controller.Admininfo.ResCode;
import com.test.controller.Admininfo.ResData;
import com.test.entity.DepartInfo;
import com.test.entity.JobInfo;
import com.test.dao.JobInfoDao;
import com.test.service.JobInfoService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 职位(JobInfo)表服务实现类
 *
 * @author makejava
 * @since 2022-11-12 11:01:47
 */
@Service("jobInfoService")
public class JobInfoServiceImpl implements JobInfoService {
    @Resource
    private JobInfoDao jobInfoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public JobInfo queryById(Integer id) {
        return this.jobInfoDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param jobInfo 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public ResData queryByPage(JobInfo jobInfo, PageRequest pageRequest) {
        int total =Integer.parseInt(String.valueOf( this.jobInfoDao.count(jobInfo)));
        List<JobInfo> jobInfos = jobInfoDao.queryAllByLimit(jobInfo, pageRequest);
        int pageSize = pageRequest.getPageSize();
        int page;
        page = (total%pageSize==0)? total/pageSize:total/pageSize+1;
        List<MyObject> myObjectList = jobInfos.stream().map(departInfo1 -> new MyObject(departInfo1)).collect(Collectors.toList());
        return new ResData(ResCode.SUCCESS,myObjectList,total,page);
    }

    /**
     * 新增数据
     *
     * @param jobInfo 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(JobInfo jobInfo) {
        return this.jobInfoDao.insert(jobInfo);
    }

    /**
     * 修改数据
     *
     * @param jobInfo 实例对象
     * @return 实例对象
     */
    @Override
    public JobInfo update(JobInfo jobInfo) {
        this.jobInfoDao.update(jobInfo);
        return this.queryById(jobInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.jobInfoDao.deleteById(id) > 0;
    }

    /**
     * 查询所有职位
     * @return
     */
    @Override
    public List<JobInfo> selectAll() {
        return jobInfoDao.selectAll();
    }

    /**
     * 根据id查职位信息
     * @param id
     * @return
     */
    @Override
    public String selectAllById(Integer id) {
        return jobInfoDao.selectAllById(id);
    }
}
